#include "videostream_core.h"
int
VSFrameQueue::Init(int width, int height)
{
	read_pos = 0;
	write_pos = 0;
    length = width * height * 3;
    for(int j = 0; j < MAX_TASK_QUEUE_CNT; j++)
    {
        if(frame[j].empty())
        {
			frame[j].create(height, width, CV_8UC3);
			rtsprintf("buffer %d malloc success.\n", j);
        }
    }

}

int 
VSFrameQueue::ReSize(int width, int height)
{
	length = width * height * 3;
	for (int j = 0; j < MAX_TASK_QUEUE_CNT; j++)
	{
		frame[j].release();
		frame[j].create(height, width, CV_8UC3);
		rtsprintf("buffer %d malloc success.\n", j);
	}
}

void
VSFrameQueue::Release()
{
    for(int j = 0; j < MAX_TASK_QUEUE_CNT; j++)
    {
        if(!frame[j].empty())
        {
			frame[j].release();
			rtsprintf("buffer %d release success.\n", j);
        }
    }

}

void
VSFrameQueue::Push(BYTE* src)
{
    int next_pos = write_pos + 1;
    if(next_pos >= MAX_TASK_QUEUE_CNT) next_pos = 0;
    if(next_pos == read_pos)
    {
        //rtsprintf(":---> frame queue is full\n");
        return;
    }
	rtsprintf("push :---> w: %2d, r : %2d, %d\n", next_pos, read_pos,  MAX_TASK_QUEUE_CNT);
    memcpy(frame[write_pos].data, src, length);
    write_pos = next_pos;
}

cv::Mat
VSFrameQueue::Pop()
{  
    cv::Mat oframe;
    if(read_pos == write_pos)
    {
        //rtsprintf(":---> frame queue is empty\n");
        return oframe;
    }
	rtsprintf("pop  :---> w: %2d, r : %2d, %d\n", write_pos, read_pos,  MAX_TASK_QUEUE_CNT);
	oframe = frame[read_pos];
    int next_pos = read_pos + 1;
    read_pos  = (next_pos >= MAX_TASK_QUEUE_CNT) ? 0 : next_pos;
    return oframe;
}



